package net.realsee.service.wcs.intf.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.intf.WcsIntfData;
import net.realsee.domain.wcs.intf.bo.WcsIntfDataBo;
import net.realsee.domain.wcs.intf.vo.WcsIntfDataVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.intf.WcsIntfDataMapper;
import net.realsee.service.wcs.intf.IWcsIntfDataService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 接口中大数据包Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsIntfDataServiceImpl extends ServiceImpl<WcsIntfDataMapper, WcsIntfData> implements IWcsIntfDataService {

    private final WcsIntfDataMapper baseMapper;

    /**
     * 查询接口中大数据包
     */
    @Override
    public WcsIntfDataVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询接口中大数据包列表
     */
    @Override
    public TableDataInfo<WcsIntfDataVo> selectVoPage(WcsIntfDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsIntfData> lqw = buildVoQueryWrapper(bo);
        Page<WcsIntfDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询接口中大数据包列表
     */
    @Override
    public List<WcsIntfDataVo> selectVoList(WcsIntfDataBo bo) {
        LambdaQueryWrapper<WcsIntfData> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsIntfData> buildVoQueryWrapper(WcsIntfDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsIntfData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsIntfData::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), WcsIntfData::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsIntfData::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsIntfData::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsIntfData::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsIntfData::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsIntfData::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsIntfData::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsIntfData::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsIntfData::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsIntfData::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsIntfData::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsIntfData::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsIntfData::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsIntfData::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增接口中大数据包
     */
    @Override
    public Boolean insertByBo(WcsIntfDataBo bo) {
        WcsIntfData add = BeanUtil.toBean(bo, WcsIntfData.class);
        return this.save(add);
    }

    /**
     * 通过数据内容新增
     */
    @Override
    public Long insertByContent(String content) {
        WcsIntfData add = new WcsIntfData();
        add.setContent(content);
        this.save(add);
        return add.getId();
    }

    /**
     * 修改接口中大数据包
     */
    @Override
    public Boolean updateByBo(WcsIntfDataBo bo) {
        WcsIntfData update = BeanUtil.toBean(bo, WcsIntfData.class);
        return this.updateById(update);
    }

    /**
     * 批量删除接口中大数据包
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
